#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<vector>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define rep(i,a,b) for(register int i = (a);i <= (b);++i)
#define per(i,a,b) for(register int i = (a);i >= (b);--i) 
#define mkp std::make_pair
typedef long long ll;
typedef unsigned long long ull;
using std::string;using std::cin;using std::cout;
inline bool cmp(int x,int y){return x < y;}

const int N = 1e6+9;
const int inf = 1e9+9;
const double eps = 1e-7;
int _,n,m,a[2*N];

inline int solve(int l,int r){
    int p = 0 , minn;
    if(r < l) return 0;
    if(l == r) return a[l] != 0;
    rep(i,l,r) if(a[p] > a[i]) p = i , minn = a[p];
    rep(i,l,r) a[i] -= minn;
    return std::min(r-l+1,solve(l,p-1)+solve(p+1,r)+minn);
}

int main(){
    std::ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //freopen("in.in", "r", stdin);

    cin >> n;
    a[0] = 2*inf;
    rep(i,1,n) cin >> a[i];
    cout << solve(1,n) << "\n";

    return 0;
}